package com.example.administrator.szb.fragments.fragment_forTab.home.adapter;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.szb.R;
import com.example.administrator.szb.fragments.fragment_forTab.home.bean.Supeis;
import com.example.administrator.szb.util.QTLog;

import java.util.List;
import java.util.Map;

/**
 * Created by stone on 2018/1/27.
 */

public class GalleryAdapter  extends BaseAdapter {
    private Context context;
    private List<Supeis> list;
    LayoutInflater inflater;
    private int select = 0;
    private int count=3;
    public GalleryAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    public void setSelect(int select) {
        this.select = select;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
        //return Integer.MAX_VALUE;
    }

    @Override
    public  Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_supei_card, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        int n = list.get(position).getFirst().size();
        holder.change(n);
        holder.title.setText("" + list.get(position).getTitle());
        holder.content.setText(list.get(position).getOther_require());
        List<Map<String, String>> l = list.get(position).getFirst();
        for (int j = 0; j < l.size(); j++) {
            Map<String, String> map = l.get(j);
            for (Map.Entry<String, String> entry : map.entrySet()) {
                if (j==0){
                    holder.tv_value_first.setText(entry.getValue());
                    holder.tv_key_first.setText(entry.getKey());
                }else if (j==1){
                    holder.tv_value_two.setText(entry.getValue());
                    holder.tv_key_two.setText(entry.getKey());
                }
                QTLog.e(entry.getKey() + ":" + entry.getValue());
            }

        }

            return convertView;

    }

    class ViewHolder{

        CardView cardView ;
        TextView title,content;
        LinearLayout ll_content;
        LinearLayout ll_first;
        TextView tv_value_first;
        TextView tv_key_first;
        LinearLayout ll_two;
        TextView tv_value_two;
        TextView tv_key_two;

        public ViewHolder(View view)
        {
            cardView = view.findViewById(R.id.cardView);
            title = view.findViewById(R.id.title);
            content = view.findViewById(R.id.content);
            ll_content = (LinearLayout) view.findViewById(R.id.ll_content);
            ll_first = (LinearLayout) view.findViewById(R.id.ll_first);
            tv_value_first = (TextView) view.findViewById(R.id.tv_value_first);
            tv_key_first = (TextView) view.findViewById(R.id.tv_key_first);
            ll_two = (LinearLayout) view.findViewById(R.id.ll_two);
            tv_value_two = (TextView) view.findViewById(R.id.tv_value_two);
            tv_key_two = (TextView) view.findViewById(R.id.tv_key_two);
        }

        public void change(int size){
            ll_two.setVisibility(View.GONE);
            content.setVisibility(View.GONE);
            ll_content.setVisibility(View.GONE);
            ll_first.setVisibility(View.GONE);

            if (size>0){
                ll_content.setVisibility(View.VISIBLE);
                ll_first.setVisibility(View.VISIBLE);
                if (size>1){
                    ll_two.setVisibility(View.VISIBLE);
                }
            }else {
                content.setVisibility(View.VISIBLE);
            }
        }

    }
}
